package com.lemon.ui.codemaker.JacksonParser;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.project.makeservice.beanparser.PackageChoicer;
import com.lemon.components.ant.project.makeservice.beanparser.PackageChoicer.PackageInfo;
import com.lemon.ui.codemaker.dto.ListItem;

public class PackageSelect extends JDialog {
	
	private static final long serialVersionUID = 6818226548481690499L;
	private String selectedValue;
	private String mavenModal;
	
	private JList<String> list;
	
	/**
	 * Create the dialog.
	 */
	public PackageSelect(String mavenModal) {
		this.mavenModal = mavenModal;
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("选择包");
		setType(Type.UTILITY);
		setBounds(100, 100, 409, 425);
		getContentPane().setLayout(new BorderLayout());
		{
			JScrollPane scrollPane = new JScrollPane();
			getContentPane().add(scrollPane, BorderLayout.CENTER);
			{
				PackageChoicer packageChoicer = new PackageChoicer ();
				List<PackageInfo> packages = packageChoicer.packageList(mavenModal);
				DefaultListModel<String> model = new DefaultListModel<>();
				if(packages == null) {
					return;
				}
				for(PackageInfo file:packages) {
					model.addElement(file.getPackageName());
				}
				list = new JList<String>(model);
				list.setCellRenderer(new ListItem());
				list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				list.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent even) {
						if(even.getClickCount() == 2) {
							setSelectedValue(list.getSelectedValue());
							dispose();
						}
					}
				});
				
				scrollPane.setViewportView(list);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if(StringUtils.isBlank(list.getSelectedValue())) {
							JOptionPane.showMessageDialog(null, "请选择包", "警告", JOptionPane.WARNING_MESSAGE);
							return;
						}
						setSelectedValue(list.getSelectedValue());
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public String getMavenModal() {
		return mavenModal;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setMavenModal(String mavenModal) {
		this.mavenModal = mavenModal;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

}
